# %%

# -*- coding: utf-8 -*-
"""
Created on Sun May 20 13:44:23 2018
@author: M
"""
# import pandas as pd
# import tushare as ts
# from matplotlib.pylab import date2num
# import datetime
# # import re,urllib2,time,csv,datetime
# import matplotlib as mpl
# import matplotlib.pyplot as plt
# import matplotlib.finance as mpf
# import matplotlib.dates as mpd
#
# code = '002911'
# start_data = '2020-01-05'
# end_data = '2020-01-23'
# hist_data = ts.get_hist_data(code, start=start_data, end=end_data)
# hist_data['时间'] = pd.to_datetime(hist_data.index, format="%Y/%m/%d")
# print(hist_data.info())
# # hist_data['时间']=hist_data['时间'].values
# # apply(lambda x:dates.date2num(x)*1440)
# print(hist_data.info())
# data_list = []
# data_list_t1 = []
# data_list_t2 = []
# for dates, row in hist_data.iterrows():
#     date_time = datetime.datetime.strptime(dates, '%Y-%m-%d')
#     t = date2num(date_time)
#     open, high, close, low = row[:4]
#     v = row[4:5]
#     # print(v)
#     datas = (t, open, high, low, close)  # tushare里的数据顺序为open,high,close,low注意
#     data_list.append(datas)
#     datas_t1 = (t)
#     data_list_t1.append(datas_t1)
#     datas_t2 = (v)
#     data_list_t2.append(datas_t2)
#
# ax1 = plt.subplot2grid((4, 4), (0, 0), rowspan=3, colspan=4)
# mpf.candlestick_ohlc(ax1, data_list, width=0.7, colorup='r', colordown='green', alpha=.4)
# plt.grid()
# ax2 = plt.subplot2grid((4, 4), (3, 0), rowspan=1, colspan=4)
# # print(data_list_t)
# # ax2.bar(data_list_t1,data_list_t2,width=0.7)
# ax2.bar(hist_data['时间'].map(date2num), hist_data['volume'], width=0.7)
# plt.grid(True)
#
# plt.show()


# %%

# -*- coding: utf-8 -*-
"""
Created on Sun May 20 13:44:23 2018
@author: M
"""
from funcat import *
import tushare as ts
from matplotlib.pylab import date2num
import datetime
# import re,urllib2,time,csv,datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
import matplotlib.dates as mpd
import re
# 显示
code = '002945'
start_data = '2019-10-05'
end_data = '2020-01-23'
hist_data = ts.get_hist_data(code, start=start_data, end=end_data)
a = hist_data[::-1]
# print(a)
# print(a.ma20[-5],a.volume[-5])
# print(a.loc['2020-01-17','close']>a.loc['2020-01-17','ma20'])
# print(a.loc[a.index[-5],'open'])


clop = (a.close / a.close.shift(1) - 1) * 100
a['clop'] = clop
a['lag1clop'] = clop.shift(1)
a['lag2clop'] = clop.shift(2)
b = a[(a.lag2clop <= 0) & (abs(a.lag1clop) < 3) & (a.clop > 2.5) & (a.clop > abs(a.lag2clop * 0.5))].index.tolist()
a["xx"] = a.index
a["xx"] = a.xx.shift(-1)
print(a, b)
print(a.iloc[0])
if re.match(r'^(3|0).*', code):
    i_code_temp = code + '.XSHE'
if re.match(r'^6.*', code):
    i_code_temp = code + '.XSHG'

    # print(b[len(b)-1])
S(i_code_temp)
T(b[len(b) - 1])
import numpy as np
import talib



# close = np.asarray(data["close"].values)
ma5 = talib.MA(hist_data.open[1:10],timeperiod=5)
print(ma5)